POW.nicename = "Teleportation V2"
POW.type = "Fire"
POW.help = "Look in a dir and press shift to teleport up to 50ft, even through walls."
POW.delay = 1
POW.range = 600

function POW:KeyPress( key )
	if( key == IN_SPEED && self.nextfire < CurTime() ) then
		local dir = self.owner:GetAimVector()
		local pos = self.owner:GetPos() + Vector(0,0,37)
		local Center = pos + dir * self.range
		local issafe = self:SafeToTeleport( Center )
		
		local counter = 1
		while !issafe do
			if( (self.range - (43*counter)) < 0 ) then break end
			Center = pos + dir * (self.range - (43*counter))
			issafe = self:SafeToTeleport( Center )
			counter = counter + 1
		end
		if( !issafe ) then return end
		
		self.owner:SetPos( (Center + Vector(0,0,-37)) )
		--self.owner:SetLocalVelocity( Vector( 0, 0, 0 ) )
		self.nextfire = CurTime() + self.delay
	end
end

function POW:KeyRelease( key )
end

function POW:Initialize()
	Msg("Teleportation2 has been loaded!\n")
end

function POW:Disabled()
	self.owner:PrintMessage(3, self.owner:Name().. " no longer has " ..self.nicename.. ".")
end

function POW:Enabled()
	self.owner:PrintMessage(3, self.owner:Name().. " now has " ..self.nicename.. ".")
end

function POW:Think()
end

function POW:SafeToTeleport( center )

	--First off, return false if the point to test isn't even in the world.
	if( !util.IsInWorld( center ) ) then return false end

	--Find out all four points of the bottom of our bounding box.
	local bp1 = center + Vector(-17,-17,-37)
	local bp2 = center + Vector(-17,17,-37)
	local bp3 = center + Vector(17,17,-37)
	local bp4 = center + Vector(17,-17,-37)

	--Find out all four points of the back of our bounding box.
	local rp1 = center + Vector(17,-17,37)
	local rp2 = center + Vector(17,17,37)
	--local fp3 = bp3 --Rear point 3 is the same point as bottom point 3.
	--local fp4 = bp4 --Rear point 4 is the same point as bottom point 4.

	--Find out the two top front points of our bounding box.
	local fp1 = center + Vector(-17,-17,37)
	local fp2 = center + Vector(-17,17,37)

	--Do four traces all up the corners of our bounding box.
	local traceRes1 = util.QuickTrace( bp1, Vector(0, 0, 74), {self.owner})
	local traceRes2 = util.QuickTrace( bp2, Vector(0, 0, 74), {self.owner})
	local traceRes3 = util.QuickTrace( bp3, Vector(0, 0, 74), {self.owner})
	local traceRes4 = util.QuickTrace( bp4, Vector(0, 0, 74), {self.owner})
	--Return false if we hit anything.
	if( traceRes1.Hit or traceRes2.Hit or traceRes3.Hit or traceRes4.Hit ) then return false end

	--Do four traces forword from the back points.
	traceRes1 = util.QuickTrace( rp1, Vector(-34, 0, 0), {self.owner})
	traceRes2 = util.QuickTrace( rp2, Vector(-34, 0, 0), {self.owner})
	traceRes3 = util.QuickTrace( bp3, Vector(-34, 0, 0), {self.owner}) --Rear point 3 is the same point as bottom point 3.
	traceRes4 = util.QuickTrace( bp4, Vector(-34, 0, 0), {self.owner}) --Rear point 4 is the same point as bottom point 4.
	--Return false if we hit anything.
	if( traceRes1.Hit or traceRes2.Hit or traceRes3.Hit or traceRes4.Hit ) then return false end
	
	--Do four traces diagonally inside our boinding box.
	traceRes1 = util.QuickTrace( rp1, rp1-bp2, {self.owner})
	traceRes2 = util.QuickTrace( rp2, rp2-bp1, {self.owner})
	traceRes3 = util.QuickTrace( fp1, fp1-bp3, {self.owner})
	traceRes4 = util.QuickTrace( fp2, fp2-bp4, {self.owner})
	--Return false if we hit anything.
	if( traceRes1.Hit or traceRes2.Hit or traceRes3.Hit or traceRes4.Hit ) then return false end
	
	--If after all the traces we have not hit anything, it's safe to teleport to this location.
	return true
	
end
